package ext.tianma.validators;

import java.rmi.RemoteException;
import java.util.Locale;

import wt.fc.WTObject;
import wt.fc.WTReference;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.session.SessionHelper;
import wt.type.TypedUtility;
import wt.util.WTException;

import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationResultSet;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.com.iba.IBAUtil;

public class SecondSupplierValidator extends DefaultUIComponentValidator{
	private static final String NO_STR = "否";
	private static final String D_VIEW = "Design";
	public UIValidationResultSet performFullPreValidation(UIValidationKey uivalidationkey, UIValidationCriteria uivalidationcriteria, Locale locale) throws WTException {
		UIValidationStatus uivalidationstatus = UIValidationStatus.HIDDEN;
		UIValidationResultSet uivresultSet = new UIValidationResultSet();
		boolean flag = false;
		
		if (uivalidationcriteria != null && uivalidationcriteria.getContextObject() != null) {
			WTReference wtreference = uivalidationcriteria.getContextObject();
			wt.fc.Persistable persistable = wtreference.getObject();
			WTUser curUser = (WTUser) SessionHelper.manager.getPrincipal();
			WTPart part = (WTPart) persistable;
			if(isObjectType(part,"SemiFinishedGood")||isObjectType(part,"C4")||isObjectType(part,"RawMaterial")||isObjectType(part,"Polarizer")||isObjectType(part,"PhantomPolarizer")||isObjectType(part,"CF"))
			{
				String state = part.getLifeCycleState().toString();
				//第二供应商 新建物料 入口打开  ||state.equals("NewPart")
				if(state.equals("INWORK")||state.equals("NewProduct")||state.equals("APPROVAL")||state.equals("MassProductionRelease"))
				{
					
					try 
					{
						String isSecondSupplier = (String) IBAUtil.getIBAValue(part, "isSecondSupplier");
						System.out.println(isSecondSupplier) ;
						System.out.println(NO_STR) ;
						if(isSecondSupplier.equals(NO_STR) )
						{
							flag = true;
						}
						//add kwang 2013-12-04 判断part是否为“Desgin”视图，如果不是则不开启入口
						String partView = part.getViewName();					
						if( partView != null && ( !partView.equals(D_VIEW) ) ){
							flag = false;
						}
					}
					catch (RemoteException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
		}
		if (flag)
		{
			uivresultSet.addResult(new UIValidationResult(uivalidationkey, UIValidationStatus.ENABLED));
		}
		else 
		{
			uivresultSet.addResult(new UIValidationResult(uivalidationkey, UIValidationStatus.HIDDEN));
		}
		return uivresultSet;
	}

	/**
	 *鍒ゆ柇瀵硅薄绫诲瀷鏄惁涓烘寚瀹氱殑鍐呴儴鍏抽敭瀛楃被鍨�
	 * @param object 瀵硅薄锛�
	 * @param object 鍐呴儴鍏抽敭瀛楋紱
	 * @return boolean  鏄寚瀹氱殑绫诲瀷杩斿洖true锛涗笉鏄繑鍥瀎alse
	 */
	public static boolean isObjectType(WTObject object,String objectType)
	{
		if(objectType.equals("") || objectType==null||object==null)
			return false;
		TypeIdentifier typeidentifier = TypedUtility.getTypeIdentifier(object);
		String typeName= typeidentifier.getTypename();
		if(typeName.indexOf(objectType)>=0)
		{
			return true;
		}
		return false;
	}

}


